<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    public function index(Request $request){
        $base         = 2000; //交易者基数
        $counts       = DB::table('waihui_member')->count(); //网站注册总人数
        $counts       = $base + $counts;
        $baseprofit   = 100000; //赚到利润基数
        $totalprofit  = DB::table('waihui_gendan')
                            ->select(DB::raw("SUM(profit) as totalprofit"))
                            ->where(array('isstop'=>0))
                            ->get()->toArray(); //所有订阅总利润
        $totalprofit  = abs($totalprofit[0]->totalprofit)+$baseprofit;
        $session_data = $request->session()->get('eai');
        //段位榜
        $where['m.isanalyst'] = '1';
        $end_start_time        = 0;
        $where['o.ispersonal']= 0; //网站下单的
        $where['o.isstop']    = 0; //已平仓的
        $gradedata            = DB::table('waihui_member as m')
                                    ->join('waihui_order as o','m.tel','=','o.tel')
                                    ->select('m.id','m.tel','m.star','m.grade','m.isvip','m.jifen','m.username','m.photo',DB::raw("(SUM(final_point)+COUNT(o.id)*5) AS final_point"),DB::raw("(count(case when o.trade_status='1' then 1 end) / count(o.tel))*100 AS winrate"))
                                    ->where($where)
                                    ->whereBetween('o.end_time', [$end_start_time, strtotime('now')])
                                    ->havingRaw('(SUM(final_point)+COUNT(o.id)*5) > 0')
                                    ->havingRaw('COUNT(o.id) > 10')
                                    ->orderBy('m.grade','asc')
                                    ->orderBy('m.star','desc')
                                    ->orderBy('final_point','desc')
                                    ->groupBy('m.tel')
                                    ->limit(3)->get();
        $gradedata            = IndexController::followerinfo($request,$gradedata)->toArray();
        //总盈利榜
        $singlepointdata      = DB::table('waihui_member as m')
                                    ->join('waihui_order as o','m.tel','=','o.tel')
                                    ->select('m.id','m.tel','m.star','m.grade','m.isvip','m.jifen','m.username','m.photo',DB::raw("(SUM(final_point)+COUNT(o.id)*5) AS final_point"),DB::raw("((SUM(final_point)+COUNT(o.id)*5)/count(o.tel)) AS single_final_point"),DB::raw("(count(case when o.trade_status='1' then 1 end) / count(o.tel))*100 AS winrate"))
                                    ->where($where)
                                    ->whereBetween('o.end_time', [$end_start_time, strtotime('now')])
                                    ->havingRaw('(SUM(final_point)+COUNT(o.id)*5) > 0')
                                    ->havingRaw('COUNT(o.id) > 10')
                                    ->orderBy('final_point','desc')
                                    ->orderBy('m.grade','asc')
                                    ->orderBy('m.star','desc')
                                    ->groupBy('m.tel')
                                    ->limit(3)->get();
        $singlepointdata       = IndexController::followerinfo($request,$singlepointdata)->toArray();
            //胜率榜
        $winratedata           = DB::table('waihui_member as m')
                                    ->join('waihui_order as o','m.tel','=','o.tel')
                                    ->select('m.id','m.tel','m.star','m.grade','m.isvip','m.jifen','m.username','m.photo',DB::raw("(SUM(o.final_point)+COUNT(o.id)*5) AS final_point"),DB::raw("(count(case when o.trade_status='1' then 1 end) / count(o.tel))*100 AS winrate"))
                                    ->where($where)
                                    ->whereBetween('o.end_time', [$end_start_time, strtotime('now')])
                                    ->havingRaw('(SUM(o.final_point)+COUNT(o.id)*5) > 0')
                                    ->havingRaw('COUNT(o.id) > 10')
                                    ->orderBy('winrate','desc')
                                    ->orderBy('m.grade','asc')
                                    ->orderBy('m.star','desc')
                                    ->groupBy('m.tel')
                                    ->limit(3)->get();
        $winratedata          = IndexController::followerinfo($request,$winratedata)->toArray();
        $nextstartdata        = IndexController::analystList('2')->toArray(); //明日之星榜
        $analystlistdata      = IndexController::analystList('1')->toArray(); //策略大师榜
        $userinfo = DB::table('waihui_member as m')
            ->select('m.username','m.sex','m.email','m.birthday','m.server_name')
            ->where('m.id','=',$session_data['id'])
            ->first();
        $userinfos = DB::table('waihui_member_info as i')->select('i.province','i.city','i.country')->where('i.userid','=',$session_data['id'])->first();
        if(!$userinfos){
            $infofinish = 1; //未完善资料
        }else{
            if($userinfo->username  && $userinfo->sex  && $userinfo->email && $userinfo->birthday  && $userinfo->server_name  && $userinfos->province && $userinfos->city && $userinfos->country ){
                $infofinish = 0;  //已完善资料
            }else{
                $infofinish = 1; //未完善资料
            }
        }
//        dd($winratedata);
        return view('welcome',compact('counts','totalprofit','singlepointdata','gradedata','winratedata','nextstartdata','analystlistdata','infofinish'));
    }
    //分析师订阅展示的信息
    public static function followerinfo(Request $request,$data){
        foreach ($data as $gkey=>$gval){
            //近一日收益点位
            $end_start_time                = strtotime("-1 week");
            $weekprofit                    = DB::table('waihui_order')->select(DB::raw('(SUM(final_point)+COUNT(id)*5) as countmoney'))->where(array('tel'=>$gval->tel,'isstop'=>0,'ispersonal'=>0))->whereBetween('end_time', [$end_start_time, strtotime('now')])->get()->toArray();
            $data[$gkey]->countmoney       = $weekprofit[0]->countmoney?$weekprofit[0]->countmoney:0;
            //获取订阅该分析师的所有用户的手机号
            $followtelarr = DB::table("waihui_follow as f")->join("waihui_member as m",'m.id','f.follower_id')->where(array('f.analyst_id'=>$gval->id,'f.status'=>1))->pluck('m.tel');
            //订阅盈利
            $followcountprofit             = DB::table('waihui_gendan as g')
                                                ->select(DB::raw('(SUM(final_point)+COUNT(o.id)*5) as followcountprofit'))
                                                ->join('waihui_order as o','g.tradeid','o.tradeid')
                                                ->where(array('g.analyst_tel'=>$gval->tel,'o.isstop'=>0,'o.ispersonal'=>0))->whereIn('g.follower_tel',$followtelarr)->get()->toArray();
            $data[$gkey]->followcountprofit=$followcountprofit?$followcountprofit[0]->followcountprofit:0;
            $data[$gkey]->fansnum          = BangdanController::followCount($gval->id); //订阅该分析师的总数
            $data[$gkey]->followerdata     = DB::table('waihui_follow as f')->select('m.photo')->join('waihui_member as m','m.id','f.follower_id')->where(array('analyst_id'=>$gval->id,'status'=>1))->limit(9)->get()->toArray();
            //是否订阅了该分析师
            $session_data                  = $request->session()->get('eai');
            if($session_data){
                $fllowinfo                 = DB::table('waihui_follow')
                                                ->select('status','direction')
                                                ->where([['analyst_id','=',$gval->id],['follower_id','=',$session_data['id']]])
                                                ->get()->toArray();
                if($fllowinfo){
                    if($fllowinfo[0]->status){
                        $data[$gkey]->status    = 1;//已订阅
                        $data[$gkey]->direction = $fllowinfo[0]->direction;//订阅方向
                    }else{
                        $data[$gkey]->status    = 0;//取消订阅
                        $data[$gkey]->direction = 0;//取消订阅
                    }
                }else{
                    $data[$gkey]->status        = 3;//未订阅
                    $data[$gkey]->direction     = 3;//未订阅
                }
            }else{
                $data[$gkey]->status            = 4; //未登录
                $data[$gkey]->direction         = 4; //未登录
            }
        }
        return $data;
    }
    //策略大师榜
    public static function analystList($type){ //'2':明日之星榜 '1':策略大师榜
        $where['m.isanalyst'] = 1;
        $where['o.isstop']    = 0;
        $where['o.ispersonal'] = 0;
        if($type == '1'){
            $whe = '=';
        }else{
            $whe = '!=';
        }
        $stardata = DB::table('waihui_member as m')
                        ->join('waihui_order as o','m.tel','o.tel')
                        ->select('m.id','m.tel','m.username','m.grade','m.star','m.photo',DB::raw('(SUM(o.final_point)+COUNT(o.id)*5) as final_point'))
                        ->where($where)
                        ->where('m.authstatus',$whe,2)
                        ->havingRaw('(SUM(o.final_point)+COUNT(o.id)*5) > 0')
                        ->havingRaw('COUNT(o.id) > 10')
                        ->orderBy('final_point','desc')
                        ->orderBy('m.grade','asc')
                        ->orderBy('m.star','desc')
                        ->groupBy('m.tel')
                        ->limit(4)->get();
        return $stardata;
    }
    //交易动态
    public static function dealTrends(){
        $dealtrends = DB::table('waihui_order as o')
                    ->join('waihui_member as m','o.tel','m.tel')
                    ->select('o.typename','o.begin_point','o.end_point','o.final_point','m.username','m.isanalyst','m.isvip','m.photo')
                    //->where(array('o.isstop'=>0,'o.ispersonal'=>0,'o.trade_status'=>'1'))
                    ->where([['o.isstop','=',0],['o.ispersonal','=',0],['o.trade_status','=','1'],['o.final_point','>','10']])
                    ->orderBy('end_time','desc')
                    ->limit(6)
                    ->get()
                    ->toArray();
        return json_encode($dealtrends);
    }
    //基本面
    public function news(Request $request){
        return view('news');
    }
}
